﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Threading;

using Castle.Core.Logging;

using InfoGenerateSystem.Persistence.IDao;
using InfoGenerateSystem.Core.Utils;
using InfoGenerateSystem.Domain;

namespace InfoGenerateSystem.AppView
{
    /// <summary>
    /// 资讯生成系统表图测试
    /// 
    /// 创建人:     王昊
    /// 创建时间:   2009-06-17
    /// 
    /// </summary>
    public partial class frmTestDxGrid : Form
    {
        private IGenericDao _dao = ServiceHelper.GetService(typeof(IGenericDao)) as IGenericDao;
        private ILogger _log = ServiceHelper.GetService(typeof(ILogger)) as ILogger;

        public frmTestDxGrid()
        {
            InitializeComponent();

            //MemoryStream ms = new MemoryStream();
            //gridControl1.DefaultView.SaveLayoutToStream(ms);
            //gridControl1.DefaultView.SaveLayoutToXml("C:\\xmlTest.xml");

            //gridControl1.ShowPrintPreview();

            //DevExpress.XtraPrinting.Control.PrintControl c = new DevExpress.XtraPrinting.Control.PrintControl();
            //c.PrintingSystem.ExportToImage("C:\\lala.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

            //Image i = Image.FromStream(ms);
            //i.Save("C:\\lala.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

            BindData();

            //PrintingSystem ps = new PrintingSystem();
            //PrintableComponentLink pcl = new PrintableComponentLink(ps);
            //pcl.Component = (DevExpress.XtraPrinting.IPrintable)(Object)gridControl1;
            //pcl.CreateDocument();
            //ImageExportOptions opts = new ImageExportOptions();
            //opts.Format = System.Drawing.Imaging.ImageFormat.Jpeg;
            //opts.Resolution = 72;
            //pcl.PrintingSystem.ExportToImage(@"C:\LaLa.jpg", opts);
        }

        private void BindData()
        {
            String labelID = "185";
            String stkCode = "600000";
            Int64 accountCode = 0;

            //String labelID = "96";
            //String stkCode = String.Empty;
            //Int64 accountCode = 300000000626;

            try
            {
                if (!String.IsNullOrEmpty(labelID))
                {
                    TblbLabelCase lblCase = _dao.Find("TblbLabelCase.FindByLABELID", labelID) as TblbLabelCase;
                    String procedureName = lblCase.SQLSTR;
                    if (!procedureName.StartsWith("EXEC", StringComparison.OrdinalIgnoreCase))
                    {
                        procedureName = "EXEC " + procedureName;
                    }
                    if (procedureName.Contains("@"))
                    {
                        procedureName = procedureName.Substring(0, procedureName.IndexOf('@'));
                    }

                    Hashtable htParams = new Hashtable();
                    htParams["labelID"] = labelID;
                    htParams["procedureName"] = procedureName;

                    if (!String.IsNullOrEmpty(stkCode))
                    {
                        htParams["STOCKCODE"] = stkCode;
                    }
                    if (accountCode != 0)
                    {
                        htParams["ACCOUNTCODE"] = accountCode;
                    }

                    DataSet dsSource = _dao.QueryForTable("SnagDataProcedure.GetDataByProcedureName", htParams);
                    if (dsSource.Tables[0].Columns.Contains("LABELID"))
                    {
                        dsSource.Tables[0].Columns.Remove("LABELID");
                    }

                    dsSource.Tables[0].Rows.Add(dsSource.Tables[0].NewRow());

                    //gridControl1.DataSource = dsSource.Tables[0].DefaultView;

                    //Thread taSnag;
                    //taSnag = new Thread(new ThreadStart(delegate()
                    //{
                    //    InfoGenerateSystem.Component.DataSetToImageExporter dstte = new InfoGenerateSystem.Component.DataSetToImageExporter();
                    //    //Image img = dstte.DoExportToImage(dsSource, 100);
                    //    //img.Save(@"C:\LaLa2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                    //}));
                    //taSnag.Start();
                }
            }
            catch (Exception ex)
            {
                _log.Error(labelID);
                _log.Error(ex.Message);
                _log.Error(ex.StackTrace);
            }
        }
    }
}
